"""Anonycoin --- Bitcoin simulator with anonymity throught group signature

Copyright (c) 2016 Daniel Pimentel <d4n1@member.fsf.org>

This file is part Anonycoin.

Anonycoin is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.

Anonycoin is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Anonycoin.  If not, see <http://www.gnu.org/licenses/>.
"""

from random import randrange, uniform
from shutil import move
from time import clock
from pyfiglet import Figlet
from wallet import Wallet
from network import Publisher, Subscriber
from transaction import Transaction
from miner import Miner
from deanonymity import deanonymity
from btc import btc_price


def message(message):
    """Print message and exit."""
    print(message)
    exit(0)


def run():
    """Run Bitcoin simulator."""
    ascii = Figlet(font="slant")
    print(ascii.renderText("Anonycoin"))
    print("[*] Exit")
    print("[1] Simulator")
    print("[2] De-anonymity")
    print("[3] Price")
    print("[4] About")
    choice = input("$ ")
    if choice == "1":
        print("How much wallets do you like to do?")
        try:
            choice = int(input("$ "))
        except:
            message("Not is a number!")
        if choice > 1:
            wallets = []
            for w in range(choice):
                wallet = Wallet()
                wallets += [wallet]
                wallet.start()
        else:
            message("It takes two or more wallets!")
        print("How much transactions do you like to do?")
        try:
            choice = int(input("$ "))
        except:
            message("Not is a number!")
        if choice > 0:
            networks = []
            publisher = Publisher()
            networks += [publisher]
            publisher.start()
            transactions = []
            miners = []
            for t in range(choice):
                w1 = randrange(len(wallets))
                w2 = randrange(len(wallets))
                if w1 != w2:
                    btc = round(uniform(1, 100), 2)
                    block = randrange(20)
                    if wallets[w1].btc >= btc:
                        for s in range(2):
                            start_clock = clock()
                            transaction = Transaction(wallets[w1], wallets[w2],
                                                      btc)
                            transactions += [transaction]
                            transaction.start()
                            transaction.make(s)
                            subscriber = Subscriber()
                            networks += [subscriber]
                            subscriber.start()
                            if s == 1:
                                subscriber.proxy()
                                blockname = "anonycoin-time.csv"
                            else:
                                blockname = "bitcoin-time.csv"
                            publisher.broadcast(transaction.inputs[0])
                            miner = Miner(block, transaction, subscriber.ip)
                            miners += [miner]
                            miner.start()
                            miner.pow()
                            miner.mining(s)
                            stop_clock = clock() - start_clock
                            with open(blockname, "a", encoding="utf-8") as f:
                                f.write(str(stop_clock) + "\n")
                        wallets[w1].send(wallets[w2], btc)
            for k in wallets:
                k.join()
            for k in transactions:
                k.join()
            for k in miners:
                k.join()
            message("Done!")
        else:
            message("It takes one or more transactions!")
    elif (choice == "2"):
        deanonymity()
    elif (choice == "3"):
        btc_price()
    elif (choice == "4"):
        print("""
        Anonycoin --- Bitcoin simulator with anonymity throught group signature

        Copyright (c) 2016 Daniel Pimentel <d4n1@member.fsf.org>

        This file is part Anonycoin.

        Anonycoin is free software; you can redistribute it and/or modify it
        under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 3 of the License, or (at
        your option) any later version.

        Anonycoin is distributed in the hope that it will be useful, but
        WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with Anonycoin.  If not, see <http://www.gnu.org/licenses/>.
        """)
    else:
        message("Exit!")


if __name__ == '__main__':
    run()
